<?php

/**
 * Controller_teaching
 * 班主任经验交流
 */

class Controller_exchange extends MyApp {


    /** 首页 **/
    public function actionIndex()
    {

    }
	/** 二级联动 **/
	public function actionListJson ()
	{
		$grade_id = (int)$this->get->grade_id;
		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` WHERE grade_id='{$grade_id}'");
		
		if(!empty($class_lists)){
			echo json_encode($class_lists);
		}else{
			exit("0");
		}
	}
    /** 班主任经验交流列表 **/
    public function actionList ()
    {	
		// 全部年级
		$grade_lists = $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		// 全部班级
		$class_lists = $this->db->fetchAll("SELECT `class_name`,`class_id` FROM `tbl_class`");

		// 查询条件
		$grade_id		= $this->get->grade_id;
		$class_id		= $this->get->class_id;
		$from			= $this->get->from;
		$to				= $this->get->to;
	
		$where = " ";
	
		if(!empty($grade_id)){
			$where .= " AND a.grade_id = '{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}

		if(!empty($class_id)){
			$where .= " AND a.class_id = '{$class_id}' ";
			$this->tpl->assign("class_id", $class_id);
		}

		if(!empty($from)){
			$where .= " AND a.create_time > '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		
		if(!empty($to)){
			$where .= " AND a.create_time < '{$to}' ";
			$this->tpl->assign("to", $to);
		}
    	 // 分页     
        YOU::loadLib('page', "", '0');
        $tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage` AS a,`tbl_grade` AS b WHERE a.grade_id=b.grade_id {$where}");
        $count = $tmp['count'];
        $page_total = ceil($count/PAGE_LIMIT);
        // 当前页                  
        $p = intval($_GET['p']);
        if ($p == 0){
            $p  = 1;
        }
        if ($p > 1){	
            $start = PAGE_LIMIT * ($p-1);
        }else{
            $start = 0;
        }
		// 7=班主任经验交流
        $manage_lists = $this->db->fetchAll("SELECT a.*,b.grade_name FROM `tbl_manage`AS a,`tbl_grade` AS b WHERE a.`manage_type`='7' AND a.grade_id=b.grade_id {$where} ORDER BY a.manage_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		
		foreach($manage_lists as $key=>$value){
			
			// 教师名称（提交者）
			$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
			$manage_lists[$key]['staff_name'] = $staff_data['staff_name'];
			
			// 班级	
			$class_data = $this->db->fetchRow("SELECT `class_name` FROM `tbl_class` WHERE class_id={$value['class_id']}");
			$manage_lists[$key]['class_name'] = $class_data['class_name'];
			
		}		

        $page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
        $page->next_page   = '下一页>';
        $page->pre_page    = '<上一页';
        $page->style_path  = "static/page_style";
       
	    $this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("manage_lists", $manage_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		
        $this->tpl->display('exchange/list.tpl.php');
    }
	/** 班主任经验交流添加 **/
    public function actionAdd()
    {        
		// 年级	
		$grade_lists = $this->db->fetchAll("SELECT `grade_id`,`grade_name` FROM `tbl_grade` ");
		// 班级	
		$class_lists = $this->db->fetchAll("SELECT `class_id`,`class_name` FROM `tbl_class` ");
		$grade_id = (int)$this->get->grade_id;
        $class_id = (int)$this->get->class_id;

        if ($_POST){ 
            print_r($grade_id.'aa');
			$manage_title   		= $this->post->manage_title;

            // 写入数据库
            $info = array( 
				
			"grade_id"  			=> $grade_id,
            "class_id"       		=> $class_id,
			"manage_title"       	=> $manage_title,
			// 班主任经验交流 =7
			"manage_type"       	=> 7,	
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_insert("tbl_manage", $info);
          //Helper_Client::location("添加成功", "index.php?a=exchange.list");

         }
		 $this->tpl->assign("grade_lists", $grade_lists);
         $this->tpl->assign("class_lists", $class_lists); 
         $this->tpl->display("exchange/add.tpl.php");
    }
    /** 班主任经验交流修改 **/
    public function actionUpdate()
    {   
		// 标题
        $manage_id				= (int)$this->get->manage_id;
		$grade_id     			= (int)$this->get->grade_id;
        $class_id   			= (int)$this->get->class_id;

        $manage_data = $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}'");
		// 年级	
		$grade_lists = $this->db->fetchAll("SELECT `grade_id`,`grade_name` FROM `tbl_grade` ");
		// 班级	
		$class_lists = $this->db->fetchAll("SELECT `class_id`,`class_name` FROM `tbl_class` ");

	    if (empty($manage_data)){
            Helper_Client::alert("信息不存在");
        }
		
		
        if ($_POST){ 
            
			$manage_title   		= $this->post->manage_title;                    

            // 写入数据库
            $info = array(
				
			"grade_id"  			=> $grade_id,
            "class_id"       		=> $class_id,
			"manage_title"       	=> $manage_title,				
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");

          // 跳转
          //Helper_Client::location("修改成功", "index.php?a=exchange.list");

         }

            $this->tpl->assign("manage_data", $manage_data);
			$this->tpl->assign("class_lists", $class_lists);
			$this->tpl->assign("grade_lists", $grade_lists);
	        $this->tpl->display("exchange/update.tpl.php");
    } 
	/** 班主任经验交流删除 **/
    public function actionDel()
    {
        $manage_id = $this->get->manage_id;   
        $this->db->query("DELETE  FROM `tbl_manage` WHERE manage_id='{$manage_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=exchange.list");
    }
	
}

?>